const Cp = require("../base-page")
class Nav extends Cp {
    constructor(config) {
        super()
        config = Object.assign({
            activeColor: "#44B549", current: 0, full: true, scroll: false, barInfo: {}
        }, config)


        this.setData({
            navConfig: config,
        })

        this.scrollLeft = 0
        this.addMethod("navClick", evt => {
            const { current } = evt.currentTarget.dataset
            this.setData({
                "navConfig.current": current,
            })
            const key = config.full ? ("#nav-item" + current) : ("#_nav-item" + current)
            wx.createSelectorQuery().select(key).fields({
                rect: true,
                size: true
            }, res => {
                try {
                    res.left += this.scrollLeft
                    this.setData({
                        "navConfig.barInfo": res
                    })
                } catch (err) {

                }

            }).exec()
            this.setData({ current })
            this.emit("change", current)
        })
        this.addMethod("onCNavScroll", evt => {
            this.scrollLeft = evt.detail.scrollLeft
        })

        setTimeout(() => {
            this.fromPage("navClick")({ currentTarget: { dataset: { current: config.current } } })
        }, 100)
    }

    goto(current) {
        this.fromPage("navClick")({ currentTarget: { dataset: { current } } })
    }
}
module.exports = Nav